/**
 * 
 */
package com.yidu.erp.action.zongdianPurchase;

import java.io.UnsupportedEncodingException;   
import java.math.BigDecimal;   
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yidu.common.SsmMessage;
import com.yidu.erp.model.BeyondApply;
import com.yidu.erp.model.BeyondDos;
import com.yidu.erp.model.BeyondDosingProlistmx;
import com.yidu.erp.model.BeyondPact;
import com.yidu.erp.model.BeyondProcurelist;
import com.yidu.erp.model.BeyondProlistmx;
import com.yidu.erp.model.BeyondStaff;
import com.yidu.erp.model.BeyondWare;
import com.yidu.erp.service.BeyondDrug.DosingService;
import com.yidu.erp.service.hetonghch.ApplyService;
import com.yidu.erp.service.hetonghch.PactDosinglistService;
import com.yidu.erp.service.hetonghch.PactService;
import com.yidu.erp.service.hetonghch.PactlistService;
import com.yidu.erp.service.ware.WareService;
import com.yidu.erp.service.zongdianPurchase.BeyondDosingProlistmxService;
import com.yidu.erp.service.zongdianPurchase.BeyondProcurelistService;
import com.yidu.erp.service.zongdianPurchase.BeyondProlistmxService;




/**
 * 类的描述:总店采购清单动作类
 * @author 欧阳平
 * @date: 2017年10月25日
 */
@Controller
@RequestMapping("beyondProcurelist")
public class BeyondProcurelistAction {
	
	@Resource
	private BeyondProcurelistService beyondProcurelistService;
	
	@Resource
	private ApplyService applyService;
	
	@Resource
	private PactService pactService;
	
	@Resource
	private PactDosinglistService pactDosinglistService;
	
	@Resource
	private PactlistService pactlistService;
	
	@Resource
	private WareService wareService;
	
	
	@Resource
	private DosingService dosingService;
	
	@Resource
	private BeyondDosingProlistmxService beyondDosingProlistmxService;
	
	@Resource
	private BeyondProlistmxService beyondProlistmxService;
	
	/**
	 * 显示投票用户信息
	 * @param curPage
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("showList")
	@ResponseBody
	public Map<String,Object> showList(String curPage,String title,String start,String end,HttpServletRequest request) throws UnsupportedEncodingException{
		//request.setCharacterEncoding("UTF-8");
		
		//定义一个map集合
		Map<String,Object> map=new HashMap<String,Object>();
		
		//填充标题到map中
		map.put("title", title);
		
		//填充起始时间到map中
		map.put("start",start);
		
		//填充结束时间到map中
		map.put("end", end);
		
		if(title!=null){
			title=java.net.URLDecoder.decode(title, "UTF-8");
			System.out.println("查询标题为:               "+title);
		}
		
		if(start!=null){
			start=java.net.URLDecoder.decode(start, "UTF-8");
			System.out.println("查询开始日期为:               "+start);
		}
		
		if(end!=null){
			end=java.net.URLDecoder.decode(end, "UTF-8");
			System.out.println("查询结束日期为:               "+end);
		}
		
		
		//得到总行数
		int count=beyondProcurelistService.findRowCount(map);
		
		//传入参数查询对应的集合
		List<Map> list=beyondProcurelistService.findAll(map);
		
		//迭代数据
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Map map2 = (Map) iterator.next();
			
			//得到map2中的时间数据,转换格式
			String time= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map2.get("PROLIST_TIME"));
			System.out.println("时间是:           "+time);
			
			//填充时间到map2中
			map2.put("TIME", time);
		}
		System.out.println("集合为:                             "+list);
		
		//编码
		map.put("code",0);
		
		//信息
		map.put("msg","");
		
		//行数
		map.put("count",count);
		
		//数据
		map.put("data",list);
		
		return map;
	}
	
	/**
	 * 添加总店采购清单信息
	 * @param str
	 * @param pactName
	 * @param prolistTypeName
	 * @param session
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public SsmMessage add(String str,String pactName,String prolistTypeName,HttpSession session){
		
		System.out.println("========================================宋旺是傻狗");
		System.out.println("拼接的字符串是:                         "+str);
		System.out.println("数值为:                  "+prolistTypeName);
		System.out.println("数值为:                  "+pactName);
		
		//从session中取得登录对象信息
		BeyondStaff staff=(BeyondStaff) session.getAttribute("staff");
		
		System.out.println("登录用户名为:         "+staff.getStaffName());
		//总店采购清单主键
		String sid = UUID.randomUUID().toString();
		
		//定义叠加数量
		int num=0;
		
		//定义叠加金额
		double money=0;
		
		//分割str字符串
		String[] data=str.split("&");
		//添加数据到总店采购清单表
		BeyondProcurelist beyondProcurelist=new BeyondProcurelist();
		
		//总店采购清单填入总店采购清单ID
		beyondProcurelist.setProlistId(sid);
		
		//总店采购清单填入合同ID
		beyondProcurelist.setPactId(pactName);
		
		//总店采购清单填入员工session中的员工ID
		beyondProcurelist.setStaffId(staff.getStaffId());
		
		//将总店采购清单信息填入数据库
		beyondProcurelistService.insert(beyondProcurelist);
		for(int i=0;i<data.length;i++){
			
			//得到date中指定的的字符串
			String temp=data[i];
			
			//分割字符串
			String arr[]=temp.split("#");
			System.out.println("data为:    "+arr[0]+"  "+arr[1]+"  "+arr[2]+"  "+arr[3]+"  "+arr[4]);
			
			//数量叠加
			num+=Integer.valueOf(arr[3]);
			
			//金额叠加
			money+=Double.valueOf(arr[4]);
			
			if(prolistTypeName.equals("1")){
				//添加数据到总店商品采购明细表
				BeyondProlistmx beyondProlistmx=new BeyondProlistmx();
				
				//生成总店商品采购明细ID
				String prolistmxId = UUID.randomUUID().toString();
				
				//总店商品采购明细信息填入总店商品采购明细ID
				beyondProlistmx.setProlistmxId(prolistmxId);
				
				//总店商品采购明细信息填入总店采购清单ID
				beyondProlistmx.setProlistId(sid);
				
				//总店商品采购明细信息填入药品ID
				beyondProlistmx.setWareId(arr[0]);
				
				//总店商品采购明细信息填入商品总金额
				beyondProlistmx.setProlistmxMoney(BigDecimal.valueOf(Double.valueOf(arr[4])));
				
				//总店商品采购明细信息填入商品总数量
				beyondProlistmx.setProlistmxCount(Integer.valueOf(arr[3]));
				
				//总店商品采购明细信息填入默认状态
				beyondProlistmx.setProlistmxIsvo("1");
				
				//将总店商品采购明细信息填入数据库
				beyondProlistmxService.insert(beyondProlistmx);
			}else if(prolistTypeName.equals("2")){
				//添加数据到总店配料采购明细表
				BeyondDosingProlistmx beyondDosingProlistmx=new BeyondDosingProlistmx();
				
				//生成总店配料采购明细ID
				String dosingProlistmxId = UUID.randomUUID().toString();
				
				//总店配料采购明细信息填入总店配料采购明细ID
				beyondDosingProlistmx.setDosingProlistmxId(dosingProlistmxId);
				
				//总店配料采购明细信息填入总店采购清单ID
				beyondDosingProlistmx.setProlistId(sid);
				
				//总店配料采购明细信息填入配料ID
				beyondDosingProlistmx.setDosId(arr[0]);
				
				//总店配料采购明细信息填土当前时间
				beyondDosingProlistmx.setDosingProlistmxTime(new Date());
				
				//总店配料采购明细信息填入商品总金额
				beyondDosingProlistmx.setDosingProlistmxMoney(BigDecimal.valueOf(Double.valueOf(arr[4])));
				
				//总店配料采购明细信息填入商品总能数量
				beyondDosingProlistmx.setDosingProlistmxCount(Integer.valueOf(arr[3]));
				
				//总店配料采购明细信息填入默认状态
				beyondDosingProlistmx.setDosingProlistmxIsvo("1");
				
				//将总店配料采购明细信息填入数据库
				beyondDosingProlistmxService.insert(beyondDosingProlistmx);
			}
			
				
			
			
		}
		System.out.println("数量是:     "+num+"    总金额是:    "+money);
		//添加总店采购清单
		BeyondProcurelist beyondProcurelistTwo=new BeyondProcurelist();
		beyondProcurelistTwo.setProlistId(sid);
		
		//总店采购编号
		String ssid = UUID.randomUUID().toString();
		
		//总店采购清单填入总店采购编号
		beyondProcurelistTwo.setProlistNumber(ssid);
		
		//总店采购清单填入采购类型
		beyondProcurelistTwo.setProlistType(prolistTypeName);
		//java.util.Date utilDate = new Date();                              //获取java.util.Date对象---也即当前时间
	    //java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());    //将java.util.Date类型转换成java.sal.Date类型		
		
		//总店采购清单填入商品总数量
		beyondProcurelistTwo.setProlistDpcount(num);
		
		//总店采购清单填入商品总金额
		beyondProcurelistTwo.setProlistMoney(BigDecimal.valueOf(money));
		
		//总店采购清单填入默认状态
		beyondProcurelistTwo.setProlistState("0");
		
		//总店采购清单填入默认备注
		beyondProcurelistTwo.setProlistRemark("宝宝");
		System.out.println("时间是:         "+beyondProcurelistTwo.getProlistTime());
		System.out.println("金钱:           "+beyondProcurelistTwo.getProlistMoney());
		
		//修改总店采购清单信息
		beyondProcurelistService.updateByPrimaryKeySelective(beyondProcurelistTwo);
		
		
		
		//定义一个信息类
		SsmMessage message=new SsmMessage();
		
		//状态
		message.setState(1);
		
		//信息
		message.setMes("ok");
		return message;
	}
	
	/**//**
	 * 查询药品时自动完成匹配相应的药品
	 * @param key
	 * @return
	 *//*
	@RequestMapping("queryJson")
	@ResponseBody
	public Map<String,Object> queryJson(String key){
		
		System.out.println("到后台的值:"+key);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("query", key);
		List<BeyondDos> list=beyondDosService.queryJson(key);
		
		
		List listData=Tools.transData(list);
		map.put("suggestions", listData);
		
		return map;
	}
	*/
	
	/**
	 * 查询指定配料信息
	 * @param key
	 * @return
	 */
	@RequestMapping("queryDosingInfo")
	@ResponseBody
	public BeyondDos queryDosingInfo(String dosId){
		
		System.out.println("到后台的值:"+dosId);
		
		BeyondDos beyondDos=dosingService.queryId(dosId);
		
		System.out.println("配料详细信息:             "+beyondDos.getDosName());
		
		return beyondDos;
	}
	
	/**
	 * 查询指定药品信息 
	 * @param key
	 * @return
	 */
	@RequestMapping("queryCommodityInfo")
	@ResponseBody  
	public BeyondWare queryCommodityInfo(String wareId){
		
		System.out.println("到后台的值:"+wareId);
		
		BeyondWare beyondWare=wareService.selectByPrimaryKey(wareId);
		
		System.out.println("药品名为:              "+beyondWare.getWareName());
		
		return beyondWare;
	}
	
	 
	
	/**
	 * 查询供货商信息
	 * @return
	 */
	@RequestMapping("queryApply")
	@ResponseBody
	public List<BeyondApply> queryApply(){
		System.out.println("进入了查询供货商信息的方法----------------------------");
		List<BeyondApply> list=applyService.selectApply();
		for(int i=0;i<list.size();i++){
			BeyondApply beyondApply=list.get(i);
			System.out.println("供货商名为:             "+beyondApply.getApplyName());
		}
		return list;
	}
	
	
	/**
	 * 查询药品合同
	 * @return
	 */
	@RequestMapping("queryPact")
	@ResponseBody
	public Map<String,Object> queryPact(String applyId,String pactType){
		System.out.println("进入了查询供货商药品合同的方法----------------查询ID为:       "+applyId+"  类型为:     "+pactType);
		
		//定义一个map
		Map<String,Object> map=new HashMap<String,Object>();
		
		//供货商ID存入map中
		map.put("applyId", applyId);
		
		//合同类型存入map中
		map.put("pactType",pactType);
		
		//放入map查询对应的药品合同信息
		List<BeyondPact> list=pactService.selectBeyondPact(map);
		for(int i=0;i<list.size();i++){
			BeyondPact beyondPact=list.get(i);
			System.out.println("合同名为:             "+beyondPact.getPactTitle());
			
		}
		System.out.println("合同集合为:               "+list);
		
		//定义一个map
		Map<String,Object> map2=new HashMap<String,Object>();
		
		//状态
		map2.put("status",200);
		
		//信息
		map2.put("message","result");
		
		//数据
		map2.put("data",list);
		
		return map2;
	}
	
	
	
	/**
	 * 查询配料信息 
	 * @return
	 */
	@RequestMapping("queryDos")
	@ResponseBody
	public Map<String,Object> queryDos(String pactId){
		System.out.println("进入了查询药品信息的方法,合同ID为:               "+pactId);
		//根据合同ID查询对应的配料信息
		List<Map> list=pactDosinglistService.findAll(pactId);
		
		//迭代
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			System.out.println("配料名为:          "+map.toString());
		}
		
		//定义一个map
		Map<String,Object> map=new HashMap<String,Object>();
		
		//状态
		map.put("status",200);
		
		//信息
		map.put("message","result");
		
		
		//数据
		map.put("data",list);
		return map;
	}
	
	/**
	 * 查询药品信息
	 * @return
	 */
	@RequestMapping("queryCommodity")
	@ResponseBody
	public Map<String,Object> queryCommodity(String pactId){
		System.out.println("进入了查询药品信息的方法,合同ID为:               "+pactId);
		
		//根据合同ID查询对应的药品信息
		List<Map> list=pactlistService.findAll(pactId);
		
		//迭代
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			System.out.println("药品名为:          "+map.toString());
		}
		
		//定义一个map
		Map<String,Object> map=new HashMap<String,Object>();
		
		//状态
		map.put("status",200);
		
		//信息
		map.put("message","result");
		
		//数据
		map.put("data",list);
		return map;
	}
	
	
	
}